<template>
  <!-- 导航栏 -->
  <Navbar />
  
  <div class="product-detail-container">
    <div class="container">
      <div v-if="loading" class="loading-section">
        <el-skeleton :loading="true" animated>
          <template #template>
            <div class="skeleton-layout">
              <el-skeleton-item variant="image" style="width: 400px; height: 400px;" />
              <div class="skeleton-info">
                <el-skeleton-item variant="h1" style="width: 60%;" />
                <el-skeleton-item variant="text" style="width: 80%; margin-top: 20px;" />
                <el-skeleton-item variant="text" style="width: 40%; margin-top: 20px;" />
              </div>
            </div>
          </template>
        </el-skeleton>
      </div>

      <div v-else-if="product" class="product-detail">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/" class="breadcrumb">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/products' }">商品列表</el-breadcrumb-item>
          <el-breadcrumb-item>{{ product.productName }}</el-breadcrumb-item>
        </el-breadcrumb>

        <div class="product-main">
          <!-- 商品图片 -->
          <div class="product-image-section">
            <div class="main-image">
              <div class="default-product-icon">
                <el-icon size="200"><Cpu /></el-icon>
              </div>
            </div>
          </div>

          <!-- 商品信息 -->
          <div class="product-info-section">
            <h1 class="product-title">{{ product.productName }}</h1>
            
            <div class="product-price-section">
              <span class="current-price">¥{{ product.price }}</span>
              <span class="stock-info">库存: {{ product.stock }} 件</span>
            </div>

            <div class="product-specs">
              <h3>产品规格</h3>
              <p>{{ product.specParams }}</p>
            </div>

            <div class="product-description">
              <h3>产品描述</h3>
              <p>{{ product.description }}</p>
            </div>

            <!-- 购买操作 -->
            <div class="purchase-section">
              <div class="quantity-selector">
                <span class="label">数量:</span>
                <el-input-number 
                  v-model="quantity" 
                  :min="1" 
                  :max="product.stock"
                  size="large"
                />
              </div>
              
              <div class="action-buttons">
                <el-button 
                  type="primary" 
                  size="large"
                  @click="addToCart"
                  :disabled="product.stock === 0"
                  class="add-cart-btn"
                >
                  加入购物车
                </el-button>
                <el-button 
                  type="danger" 
                  size="large"
                  @click="buyNow"
                  :disabled="product.stock === 0"
                  class="buy-now-btn"
                >
                  立即购买
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div v-else class="error-section">
        <el-empty description="商品不存在或已下架" />
        <el-button type="primary" @click="$router.push('/products')">
          返回商品列表
        </el-button>
      </div>
    </div>
    
    <!-- 浮动购物车 -->
    <FloatingCart />
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { Cpu } from '@element-plus/icons-vue'
import { productAPI } from '@/api'
import Navbar from '@/components/Navbar.vue'
import FloatingCart from '@/components/FloatingCart.vue'

export default {
  name: 'ProductDetailView',
  components: {
    Navbar,
    FloatingCart,
    Cpu
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    
    const loading = ref(false)
    const product = ref(null)
    const quantity = ref(1)
    
    const fetchProduct = async () => {
      try {
        loading.value = true
        const productId = route.params.id
        const response = await productAPI.getProductById(productId)
        if (response.success && response.product) {
          product.value = response.product
        } else {
          product.value = null
          ElMessage.error(response.message || '商品不存在')
        }
      } catch (error) {
        ElMessage.error('获取商品详情失败')
        product.value = null
      } finally {
        loading.value = false
      }
    }
    
    const addToCart = () => {
      store.dispatch('addProductToCart', {
        productId: product.value.productId,
        productName: product.value.productName,
        price: product.value.price,
        productImage: product.value.productImage,
        specParams: product.value.specParams,
        quantity: quantity.value
      })
      ElMessage.success(`已添加 ${quantity.value} 件商品到购物车`)
    }
    
    const buyNow = () => {
      addToCart()
      router.push('/cart')
    }
    
    onMounted(() => {
      fetchProduct()
    })
    
    return {
      loading,
      product,
      quantity,
      addToCart,
      buyNow
    }
  }
}
</script>

<style scoped>
.product-detail-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #0c0c0c 0%, #1a1a2e 50%, #16213e 100%);
  padding-top: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.breadcrumb {
  margin-bottom: 30px;
}

:deep(.el-breadcrumb__item .el-breadcrumb__inner) {
  color: #b0bec5;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: #64b5f6;
}

.product-main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 60px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 40px;
}

.product-image-section {
  display: flex;
  justify-content: center;
}

.main-image {
  width: 100%;
  max-width: 400px;
  height: 400px;
  border-radius: 15px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.1);
}

.main-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.default-product-icon {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.05);
}

.default-product-icon .el-icon {
  color: #64b5f6;
}

.product-info-section {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.product-title {
  color: #ffffff;
  font-size: 28px;
  font-weight: 600;
  margin: 0;
  line-height: 1.3;
}

.product-price-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
}

.current-price {
  color: #64b5f6;
  font-size: 32px;
  font-weight: 700;
}

.stock-info {
  color: #b0bec5;
  font-size: 16px;
}

.product-specs,
.product-description {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  padding: 20px;
}

.product-specs h3,
.product-description h3 {
  color: #ffffff;
  font-size: 18px;
  margin: 0 0 15px 0;
}

.product-specs p,
.product-description p {
  color: #b0bec5;
  font-size: 14px;
  line-height: 1.6;
  margin: 0;
}

.purchase-section {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 15px;
  padding: 20px;
}

.quantity-selector {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 25px;
}

.quantity-selector .label {
  color: #ffffff;
  font-weight: 500;
  min-width: 50px;
}

.action-buttons {
  display: flex;
  gap: 15px;
}

.add-cart-btn,
.buy-now-btn {
  flex: 1;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 12px;
}

.add-cart-btn {
  background: linear-gradient(135deg, #64b5f6 0%, #42a5f5 100%);
  border: none;
}

.add-cart-btn:hover {
  background: linear-gradient(135deg, #42a5f5 0%, #1e88e5 100%);
}

.buy-now-btn {
  background: linear-gradient(135deg, #ff7043 0%, #f4511e 100%);
  border: none;
}

.buy-now-btn:hover {
  background: linear-gradient(135deg, #f4511e 0%, #e64a19 100%);
}

.loading-section {
  padding: 40px;
}

.skeleton-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 60px;
}

.skeleton-info {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.error-section {
  text-align: center;
  padding: 60px 20px;
}

:deep(.el-empty__description p) {
  color: #b0bec5;
}

@media (max-width: 768px) {
  .product-main {
    grid-template-columns: 1fr;
    gap: 30px;
    padding: 20px;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .skeleton-layout {
    grid-template-columns: 1fr;
  }
}
</style> 